class CheckrecordController < ApplicationController

  def getCheckrecords
    crs = Checkrecord.find_by_train_task params[:train_task_id].to_i
    total  = crs.length
    jsonStr="{checkrecord:"+crs.to_json()+",total:"+total.to_s+"}"
    puts jsonStr
    render :text => jsonStr,:layout => false
  end

  #待修改
  def printRecord
    Spreadsheet.client_encoding = "UTF-8"
    bookString = StringIO.new
    book = Spreadsheet.open "#{RAILS_ROOT}/public/report_template/checkrecord_template.xls"
    crs = Checkrecord.find_by_train_task params[:train_task_id].to_i
    i = 1

    sheet1 = book.worksheet 0
    crs.each do |cr|
      sheet1[i,0] = cr.worker_name
      sheet1[i,1] = cr.team_name
      sheet1[i,2] = cr.train_name
      sheet1[i,3] = cr.track
      sheet1[i,4] = cr.check_in_time ? cr.check_in_time.strftime('%Y-%m-%d %H:%M') : ''
      sheet1[i,5] = cr.check_out_time ? cr.check_out_time.strftime('%Y-%m-%d %H:%M') : ''
      sheet1[i,6] = ((cr.check_out_time - cr.check_in_time)/60).to_i if cr.check_in_time && cr.check_out_time
      sheet1[i,7] = cr.dynamic_check_in_time ? cr.dynamic_check_in_time.strftime('%Y-%m-%d %H:%M') : ''
      sheet1[i,8] = cr.dynamic_check_out_time ? cr.dynamic_check_out_time.strftime('%Y-%m-%d %H:%M') : ''
      sheet1[i,9] = ((cr.dynamic_check_out_time - cr.dynamic_check_in_time)/60).to_i if cr.dynamic_check_in_time && cr.dynamic_check_out_time
      i += 1
    end
    book.write bookString
    send_data bookString.string,:filename => 'worker.xls'
  end
  
end
